<?php
	class collect{
		var $table;
		var $msg;
		
		function __construct()
		{
			
			global $db,$MODULE;
			$this->db = &$db;
			$this->table = DB_PRE.'collect';
		}
	
		function collect()
		{
			$this->__construct();
		}
		
		function listinfo($where = '', $order = 'id DESC', $page = 1, $pagesize = 50){
			global $_userid;
			$page = max($page, 1);
			$num = $this->db->get_one("SELECT COUNT(*) AS n FROM $this->table WHERE `userid`='$_userid'");
			$total = $num['n'];
			$data['pages'] = pages($total, $page, $pagesize);
			$offset = ($page-1)*$pagesize;
			$data['info'] = $this->db->select("SELECT * FROM $this->table WHERE `userid`='$_userid' AND $where ORDER BY $order LIMIT $offset, $pagesize");
			return $data;
		}
		
		function add($info){
			extract($info);
			if(!$module || !$field || !$value) $this->msg = '缺少信息参数，请重新选择要收藏的信息！';
			
			$data = $this->db->get_one("SELECT id FROM $this->table WHERE `module`= '$module' AND `field`='$field' AND `value`='$value' AND `userid`='$_userid'");
			if($data['id']) {
				$this->msg = '此信息您已经收藏！';
			}
			
			$this->db->insert($this->table, $info);
			$id = $this->db->insert_id();
			return $id ? $id : false;
		}
		
		function delete($where){
			global $_userid;
			
			// 除了管理员，其他用户只能删除自己的评论
			$where = $_userid == 1 ? $where : " $where AND `userid`= $_userid";
			return $this->db->query("DELETE FROM $this->table WHERE $where ");
		}
	}
?>